Imports IIC.Forms.Edit
Imports IICBusinessLogic

Namespace Forms.List

    ''' <summary>
    ''' Form used to list students.  Inherits from the base student list form
    ''' </summary>
    ''' <remarks></remarks>
    Public Class frmStudentList
        Inherits frmStudentListBase

        ''' <summary>
        ''' the class id that the "AddToClass" button is working with
        ''' Null at the beginning but when the frmAddStudents is loaded
        ''' and a PeriodClassId is selected, it is remembered in this variable
        ''' </summary>
        ''' <remarks></remarks>
        Private workingPeriodClass As PeriodClass

        Overridable Sub LoadDataToGrid()

            'load data to grid
            Me.grdData.loadGrid()
            Dim ismemberCol As DataGridViewAutoFilterCheckBoxColumn = _
                    CType(Me.grdData.Columns("IsMember"), DataGridViewAutoFilterCheckBoxColumn)

            Me.Text = "Students and Members List"
            
        End Sub

        Sub frmStudentList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Me.DesignMode Then Exit Sub

            Me.LoadDataToGrid()

            Me.addToolbarActionButton("Add To Class...", _
                                      AddressOf cmdAddToClass_Click, _
                                      80, My.Resources.Resources.NewAppointment)

            Me.addToolbarActionButton("Enter Grades...", _
                                     AddressOf cmdEnterGrades_Click, _
                                     80, My.Resources.Resources.EnterGrades)

            Me.addToolbarActionButton("Merge Student...", _
                                     AddressOf mergeStudent_Click, _
                                     80, My.Resources.Resources.TreeStudent)

        End Sub

        ''' <summary>
        ''' Adds the selected students to the specified period/class
        ''' </summary>
        Protected Sub cmdAddToClass_Click(ByVal sender As Object, ByVal e As EventArgs)

            If Me.grdData.SelectedRows.Count = 0 Then
                Call winUtils.MsgboxStop("Please select at least one student from the list")
            Else

                Dim newClassMembers As List(Of Student) = New List(Of Student)
                For Each row As DataGridViewRow In Me.grdData.SelectedRows

                    Dim drv As DataRowView = CType(Me.grdData.BindingSource.Item(row.Index), DataRowView)
                    Dim student As Student = StudentDataUtils.loadFromDataRow(drv.Row)
                    newClassMembers.Add(student)
                Next

                Using f As New frmAddStudents
                    f.setNewClassMembers(newClassMembers)

                    If f.ShowDialog() = DialogResult.OK Then
                        'Me.workingPeriodClass = f.periodClass
                    End If
                End Using

            End If

        End Sub

        Protected Sub cmdEnterGrades_Click(ByVal sender As Object, ByVal e As EventArgs)

            If Me.grdData.SelectedRows.Count = 0 Then
                Call winUtils.MsgboxStop("Please select at least one student from the list")
                Return
            End If


            Dim drv As DataRowView = CType(Me.grdData.BindingSource.Item(Me.grdData.SelectedRows(0).Index), DataRowView)
            Dim student As Student = StudentDataUtils.loadFromDataRow(drv.Row)


            If CType(student, Student).ClassesAttended.Count = 0 Then
                Call winUtils.MsgboxStop("The selected student " & CType(student, Student).StudentFullName & _
                                         "  does not participate in any class.")
                Return
            End If

            Dim classAttendedInOpenPeriod As ClassMember = _
                        CType(student, Student).ClassesAttended.FirstOrDefault( _
                                Function(p)
                                    Return p.PeriodClass.Period.IsOpen.GetValueOrDefault = True
                                End Function)

            If classAttendedInOpenPeriod Is Nothing Then
                Call winUtils.MsgboxStop("The selected student " & CType(student, Student).StudentFullName & _
                                         "  does not participate in any class in an open period.")
                Return
            End If

            Dim f As New frmEnterStudentGrades
            Try

                f.Text = f.Text & " - " & student.StudentFullName
                f.UcEnterStudentGrades.loadClassesAttended(student.StudentId)
                f.ShowDialog()

            Finally
                If f IsNot Nothing Then f.Dispose()
            End Try

        End Sub

        Private Sub mergeStudent_Click(ByVal sender As Object, ByVal e As EventArgs)

            If Me.grdData.SelectedRows.Count = 0 Then
                Call winUtils.MsgboxStop("To merge a person/member, please select a row from the list.")
                Exit Sub
            End If

            Dim stid As Integer = Me.grdData.IdValue
            Dim uim As New UIMergePersonSelector
            Dim stayStudent As Student = StudentDataUtils.findOne("studentid={0} ", stid)
            Dim otherStudent As Student = uim.selectMemberToMerge(stayStudent)

            If otherStudent Is Nothing Then
                Exit Sub
            End If

            Dim lNewVariable As String = String.Format("Are you sure you want to merge selected person ""{0}"" with record {1}", _
                                stayStudent.StudentFullName & _
                        ", Member No:" & CStr(IIf(stayStudent.MemberNumber.GetValueOrDefault > 0, _
                                            stayStudent.MemberNumber, "NONE")), _
                            otherStudent.StudentFullName & _
                        ", Member No:" & CStr(IIf(otherStudent.MemberNumber.GetValueOrDefault > 0, _
                                            otherStudent.MemberNumber, "NONE")))

            If winUtils.MsgboxQuestion(lNewVariable) = MsgBoxResult.No Then Exit Sub

            Dim smerger As StudentMerger = New StudentMerger
            Call smerger.merge(stayStudent, otherStudent)

            Call Me.grdData.loadGrid()
            Call Me.executeSearch(Me.tsTxtSearch.Text)

        End Sub

    End Class
End Namespace
